/**
 * ---------------------------
 * Time: 2017/8/6 23:14.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
define(
    ['app', '../../config-dao'],
    function (app, ConfigDao) {
        app.factory('configDao', function (INDEXED_DB, PromiseUtil) {

            // init
            var configDao = INDEXED_DB.configDao = new ConfigDao();
            var dexieDB = INDEXED_DB.dexieDB;
            var configs = dexieDB.configs;

            /**
             * 添加配置
             * @param key
             * @param value
             * @param cb
             */
            configDao.addConfig = function (key, value, cb) {
                console.debug('[configDao.addConfig] - adding - [' + key + ' = ' + value + ']');
                var promise = configs.add({
                    key: key,
                    value: value
                });
                PromiseUtil.parserCallBack(promise, cb, '添加配置[' + key + ' = ' + value + ']');
            };

            /**
             * 查询所有配置
             * @param cb
             */
            configDao.findAllConfig = function (cb) {
                var promise = configs.toArray();
                PromiseUtil.parserCallBack(promise, cb, '获取所有配置');
            };

            /**
             * 通过键来匹配值
             * @param key
             * @param cb
             */
            configDao.findConfigByKey = function (key, cb) {
                var collection = configs.where('key').equals(key);
                var promise = collection.toArray();
                PromiseUtil.parserCallBack(promise, cb, '获取配置中key=\'' + key + '\'的值');
            };

            /**
             * 更新配置
             * @param key
             * @param value
             * @param cb
             */
            configDao.updateConfig = function (key, value, cb) {
                var promise = configs.put({
                    key: key,
                    value: value
                });
                PromiseUtil.parserCallBack(promise, cb, '更新配置[\'' + key + '\'=\'' + value + '\']');
            };


            return configDao;
        })
    }
);